var param_platform_code = $.url().param('platform_code');
var param_type_code = $.url().param('type_code');
var param_league_code = $.url().param('league_code');
var param_left_code = $.url().param('left_code');
var param_right_code = $.url().param('right_code');
var param_play_code = $.url().param('play_code');
var param_date = $.url().param('date');
var chartSeries = [];
var chartData = [];
var myChart;
var titleText = '';

getRateCommonInfo(param_type_code, param_platform_code, param_league_code, param_left_code, param_right_code, param_play_code, param_date);

function setTeamName(team_name, score, team_kind) {
    $("#rate_team_" + team_kind + "_name_loading_zone").show();
    $("#rate_team_" + team_kind + "_name_data_zone").hide();
    $("#rate_team_" + team_kind + "_name_error_zone").hide();
    if (score === '') {
        $("#rate_team_" + team_kind + "_name_data_zone").html(team_name);
    } else {
        $("#rate_team_" + team_kind + "_name_data_zone").html(team_name + "（" + score + "）");
    }
    $("#rate_team_" + team_kind + "_name_loading_zone").hide();
    $("#rate_team_" + team_kind + "_name_error_zone").hide();
    $("#rate_team_" + team_kind + "_name_data_zone").show();
}

function setLeagueName(rate_league_name) {
    $("#rate_league_name_loading_zone").show();
    $("#rate_league_name_data_zone").hide();
    $("#rate_league_name_error_zone").hide();
    $("#rate_league_name_data_zone").html(rate_league_name);
    $("#rate_league_name_loading_zone").hide();
    $("#rate_league_name_error_zone").hide();
    $("#rate_league_name_data_zone").show();
}

function getRateCommonInfo(type_code, platform_code, league_code, left_code, right_code, play_code, date) {
    $("#rate_common_loading_zone").show();
    $("#rate_common_data_zone").hide();
    $("#rate_common_error_zone").hide();
    getRateCommonData(type_code, platform_code, league_code, left_code, right_code, play_code, date, 'Y');
    setInterval(function () {
        getRateCommonData(type_code, platform_code, league_code, left_code, right_code, play_code, date, 'N');
    }, 2000);
}

function getRateCommonData(type_code, platform_code, league_code, left_code, right_code, play_code, date, acquire_flag) {
    var timeStampPre = $("#rate_common_time_stamp").val();
    var token = getCookie('csrftoken');
    $.ajax({
        // url: '/football/get_rate_common_info/',
        url: 'http://8.134.215.241:9080/jyb/core/plat/acquireplatplayhdcps',
        type: 'POST',
        headers:{'X-CSRFToken': token, "Content-Type": "application/json"},
        data: JSON.stringify({'baseDate': date, 'platCode': platform_code, 'typeCode': type_code, 'leaCode': league_code, 'teamCodeLeft': left_code, 'teamCodeRight': right_code, 'playCode': play_code, 'acquireFlag': acquire_flag, 'timeStampPre': timeStampPre}),
        success: function(data) {
            console.log(data);
            chartSeries = data['result']['lineVoList'];
            titleText = data['result']['playName'];
            if (chartSeries.length === 0) {
                let updateData = data['result']['hdcpVoList'];
                updateData.forEach((item, index) => {
                    chartData.push(item);
                });
                myChart.setOption({
                    dataset: {
                        source: chartData
                    },
                });
            } else {
                chartData = data['result']['hdcpVoList'];
                $("#rate_common_loading_zone").hide();
                $("#rate_common_error_zone").hide();
                $("#rate_common_data_zone").show();
                initChartCommon('rate_common_data_zone', chartSeries, chartData);
            }
            $("#rate_common_time_stamp").val(data['result']['timeStampPre']);
            setLeagueName(data['result']['leaName']);
            setTeamName(data['result']['teamNameLeft'], data['result']['scoreLeft'], 'left');
            setTeamName(data['result']['teamNameRight'], data['result']['scoreRight'], 'right');
        },
        error: function() {
            $("#rate_common_loading_zone").hide();
            $("#rate_common_data_zone").hide();
            $("#rate_common_error_zone").show();
        }
    });
}

function initChartCommon(zoneId, chartSeries, chartData) {
    var chartDom = document.getElementById(zoneId);
    myChart = echarts.init(chartDom);
    var option = {
        title: { text: titleText, },
        grid: { // 控制图表的边距，从而可以调整图表大小
            left: '10',
            right: '10',
            bottom: '20',
            containLabel: true // 保证标签不被遮挡在图表外边
        },
        legend: {},
        tooltip: {},
        dataset: {
            // 提供一份数据。
            source: chartData
        },
        // 声明一个 X 轴，类目轴（category）。默认情况下，类目轴对应到 dataset 第一列。
        xAxis: {
            type: 'category',
            axisLabel: {
                rotate: 45 // 设置标签旋转90度，即竖直显示
            }
        },
        // 声明一个 Y 轴，数值轴。
        yAxis: {

        },
        // 声明多个 bar 系列，默认情况下，每个系列会自动对应到 dataset 的每一列。
        series: chartSeries
    };
    myChart.setOption(option, true);
}

function navigateToPlatformPage() {
    goToOtherPage('./platform.html?date=' + param_date );
}

function navigateToTypePage() {
    goToOtherPage('./type.html?platform_code=' + param_platform_code + '&date=' + param_date);
}

function navigateToLeaguePage() {
    goToOtherPage('./league.html?platform_code=' + param_platform_code + '&type_code=' + param_type_code + '&date=' + param_date);
}

function navigateToMatchPage() {
    goToOtherPage('./match.html?platform_code=' + param_platform_code + '&type_code=' + param_type_code + '&league_code=' + param_league_code + '&date=' + param_date);
}

function navigateToPlayPage() {
    goToOtherPage('./play.html?platform_code=' + param_platform_code + '&type_code=' + param_type_code + '&league_code=' + param_league_code + '&left_code=' + param_left_code + '&right_code=' + param_right_code + '&play_code=' + param_play_code + '&date=' + param_date);
}